package com.tcdp.daos;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.tcdp.models.Page;

public class PageDao extends AbstractDao {

	public Page findById(int id){
		Page page = em.find(Page.class, id);
		return page;
	}
	
	public Page findByUrl(String url){
		Page page = null;
		
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Page> cq = cb.createQuery(Page.class);
		Root<Page> root = cq.from(Page.class);
		cq.select(root).where(cb.equal(root.<String>get("pageUrl"), url));
		TypedQuery<Page> tq = em.createQuery(cq);
		
		try{
			page = tq.getSingleResult();
		}catch(NoResultException e){
			throw404(e);
		}
		
		return page;
	}
	
}
